package sdk.org.apache.shiro.G2_使用.C10_SpringBoot集成_1登录和退出.AppConfig.aspect;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.titan.core.base.BaseControllerV8;
import com.titan.core.utils.data.FormatUtils;
import com.titan.core.utils.io.ConsoleUtils;
import com.titan.core.web.utils.RequestUtils;

@Aspect
@Component
public class ControllerAspect {
    // @Around("execution(public * zzz..controller.*Controller.*(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse))")
    // @Around("execution(public * *..*Controller.*(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse))")
    @Around("execution(public * sdk.org.apache.shiro.G2_使用.C10_SpringBoot集成_1登录和退出.services.*Controller.*(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse))")
    public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Exception{  
        long time = System.currentTimeMillis();
        
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();
        String uri = request.getRequestURI();
        if("/titan/api".equals(uri)) {
            try {
                return proceedingJoinPoint.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        // 记录下请求内容
        StringBuffer buf = new StringBuffer(128);
        buf.append("######################################################\n");
        buf.append("日期时间：").append(FormatUtils.getSysdate("yyyy-MM-dd HH:mm:ss")).append("\n");
        buf.append("请求URL：").append(request.getRequestURL()).append("\n");
        buf.append("请求IP：").append(RequestUtils.getRemortIP(request)).append("\n");
        Signature signature = proceedingJoinPoint.getSignature();
        buf.append("执行类：").append(signature.getDeclaringTypeName()).append(" # ").append(signature.getName()).append("\n");
        System.out.println(buf);
        ConsoleUtils.printParameter(request);
        try {
            return proceedingJoinPoint.proceed();
        } catch (IllegalArgumentException e) {
            if(e.getMessage().startsWith("[2001]")) {
                throw e;
            } else {
                e.printStackTrace();
                BaseControllerV8.write(e, request, response);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            BaseControllerV8.write(e, request, response);
        } finally {
            System.out.println("执行用时 : " + (System.currentTimeMillis() - time) + "毫秒");
        }
        return null;
    }
}
